package com.baidu.tcpThreadPoolDemo1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerRunnable implements Runnable{
    //1.创建socket链接及对应构造器
    private Socket socket;

    public ServerRunnable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //2.读取输入流消息
            InputStream inputStream = socket.getInputStream();

            //3.将输入流转换成字节输入流后包装打印
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            //4.循环判断buffer中是否有字符 有就出书
            String msg;
            while ((msg = bufferedReader.readLine()) != null){
                System.out.println("线程:" + Thread.currentThread().getName() + " 客户端:" + socket.getRemoteSocketAddress() + "消息: " + msg);
            }

        } catch (Exception e) {
            System.out.println("线程:" + Thread.currentThread().getName() + " 客户端:" + socket.getRemoteSocketAddress() + " 已下线");
        }
    }
}
